package com.itheima.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.SmsHomeRecommendSubjectMapper;
import com.itheima.pojo.SmsHomeRecommendSubject;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author lenovo
 * @description 针对表【sms_home_recommend_subject(首页推荐专题表)】的数据库操作Service实现
 * @createDate 2022-10-11 14:26:18
 */
@DubboService
public class SmsHomeRecommendSubjectApiImpl implements SmsHomeRecommendSubjectApi {

    @Autowired
    private SmsHomeRecommendSubjectMapper recommendSubjectMapper;

    //添加首页推荐专题
    @Override
    public void create(SmsHomeRecommendSubject recommendSubject) {
        recommendSubjectMapper.insert(recommendSubject);
    }

    //删除推荐
    @Override
    public void delete(Integer id) {
        recommendSubjectMapper.deleteById(id);
    }

    //分页查询推荐
    @Override
    public PageResult list(Integer pageNum, Integer pageSize, String subjectName, Integer recommendStatus) {
        //分页
        IPage<SmsHomeRecommendSubject> iPage = new Page<>(pageNum, pageSize);
        //条件
        LambdaQueryWrapper<SmsHomeRecommendSubject> lqw = new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotBlank(subjectName), SmsHomeRecommendSubject::getSubjectName, subjectName)
                .eq(ObjectUtil.isNotNull(recommendStatus), SmsHomeRecommendSubject::getRecommendStatus, recommendStatus)
                .orderByAsc(SmsHomeRecommendSubject::getSort);
        recommendSubjectMapper.selectPage(iPage, lqw);
        //获取参数
        Integer total = Convert.toInt(iPage.getTotal());//总条数
        Integer totalPage = Convert.toInt(iPage.getPages());//当前分页总页数
        long size = iPage.getSize();//每页显示条数
        List<SmsHomeRecommendSubject> list = iPage.getRecords();
        return new PageResult(pageNum, pageSize, total, totalPage, list);
        //public PageResult(Integer pageNum, Integer pageSize, Integer total, Integer totalPage, List<T> list) {
    }

    //修改推荐状态
    @Override
    public void updateStatus(Long id, Integer recommendStatus) {
        SmsHomeRecommendSubject recommendSubject = new SmsHomeRecommendSubject();
        recommendSubject.setId(id);
        recommendSubject.setRecommendStatus(recommendStatus);
        int updateCount = recommendSubjectMapper.updateById(recommendSubject);
    }

    //修改推荐排序
    @Override
    public void updateSortId(Long id, Integer sort) {
        SmsHomeRecommendSubject recommendSubject = recommendSubjectMapper.selectById(id);
        if (ObjectUtil.isNotNull(recommendSubject)) {
            recommendSubject.setSort(sort);
            recommendSubjectMapper.updateById(recommendSubject);
        }
    }
}




